<template>
  <el-form label-width="100px">
    <el-form-item label="sku名称">
      <el-input placeholder="SKU名称" v-model="skuParams.skuName"></el-input>
    </el-form-item>
    <el-form-item label="价格(元)">
      <el-input placeholder="价格(元)" v-model="skuParams.price"></el-input>
    </el-form-item>
    <el-form-item label="重量(克)">
      <el-input placeholder="重量(克)" v-model="skuParams.weight"></el-input>
    </el-form-item>
    <el-form-item label="sku描述">
      <el-input type="textarea" placeholder="SKU描述" v-model="skuParams.skuDesc"></el-input>
    </el-form-item>
    <el-form-item label="平台属性">
      <!--               变为行内元素，el-select横向排列 -->
      <el-form label-width="80px" :inline="true">
        <el-form-item v-for="arr in AttrArr" :key="arr.id" :label="arr.attrName">
          <el-select v-model="arr.attrIdAndValueId">
            <el-option
              v-for="row in arr.attrValueList"
              :key="row.id"
              :label="row.valueName"
              :value="`${arr.id}:${row.id}`"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="销售属性">
      <el-form label-width="80px" :inline="true">
        <el-form-item v-for="arr in AttrList" :key="arr.id" :label="arr.saleAttrName">
          <el-select v-model="arr.saleIdAndValueId">
            <el-option
              v-for="row in arr.spuSaleAttrValueList"
              :key="row.id"
              :label="row.saleAttrValueName"
              :value="`${arr.id}:${row.id}`"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="图片名称">
      <el-table border :data="ImageList" ref="table">
        <el-table-column type="selection" width="100px" align="center"></el-table-column>
        <el-table-column label="图片">
          <!-- eslint-disable -->
          <template #="{ row, $index }">
            <img :src="row.imgUrl" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="imgName"></el-table-column>
        <el-table-column label="操作">
          <template #="{ row, $index }">
            <el-button type="primary" size="default" icon="Pointer" @click="handleImg(row)">
              设置默认
            </el-button>
          </template>
          <!-- eslint-enable -->
        </el-table-column>
      </el-table>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="default" @click="save">保存</el-button>
      <el-button type="primary" size="default" @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { ref } from 'vue'
//引入ts数据类型
import { SpuData, SpuImage, SaleAttr, SkuData } from '@/api/product/spu/type'
import { Attr } from '@/api/product/attr/type'
//引入接口
import { reqAttr } from '@/api/product/attr/index'
import { reqImageList, reqAttrList, reqAddSku } from '@/api/product/spu/index'
let $emit = defineEmits(['changeScene'])
//存放数据
//存放平台属性
let AttrArr = ref<Attr[]>([])
//存放照片数据
let ImageList = ref<SpuImage[]>([])
//存放销售属性
let AttrList = ref<SaleAttr[]>([])
//存放Sku的数据
let skuParams = ref<SkuData>({
  category3Id: '',
  spuId: '',
  tmId: '',
  skuName: '',
  price: '',
  weight: '',
  skuDesc: '',
  skuAttrValueList: [],
  skuSaleAttrValueList: [],
  skuDefaultImg: '',
})
//获取table勾选框的组件实例
let table = ref<any>()

//返回spu页面
const cancel = () => {
  console.log(1)
  Object.assign(skuParams.value, {
    category3Id: '',
    spuId: '',
    tmId: '',
    skuName: '',
    price: '',
    weight: '',
    skuDesc: '',
    skuAttrValueList: [],
    skuSaleAttrValueList: [],
    skuDefaultImg: '',
  })
  $emit('changeScene', { value: 0, params: '' })
}
//获取数据
const initSkuData = async (c1Id: number | string, c2Id: number | string, data: SpuData) => {
  let result1 = await reqAttr(c1Id, c2Id, data.category3Id)
  let result2 = await reqImageList(data.id)
  let result3 = await reqAttrList(data.id)
  if (result1.code === 200 && result2.code === 200 && result3.code === 200) {
    AttrArr.value = result1.data
    ImageList.value = result2.data
    AttrList.value = result3.data
    //收集sku属性
    skuParams.value.category3Id = data.category3Id
    skuParams.value.spuId = data.id
    skuParams.value.tmId = data.tmId
  } else {
    ElMessage({
      type: 'error',
      message: '获取数据失败 ',
    })
  }
}
//收集图片地址数据
const handleImg = (row: SpuImage) => {
  skuParams.value.skuDefaultImg = row.imgUrl
  //复选框选中
  ImageList.value.forEach((item) => {
    table.value.toggleRowSelection(item, false)
  })
  table.value.toggleRowSelection(row, true)
}
//保存按钮
const save = async () => {
  //收集之前保存的平台属性选择选项的id
  skuParams.value.skuAttrValueList = AttrArr.value.reduce((prev: any, next: any) => {
    if (next.attrIdAndValueId) {
      let [attrId, valueId] = next.attrIdAndValueId.split(':')
      prev.push({
        attrId,
        valueId,
      })
    }
    return prev
  }, [])
  //收集之前保存的销售属性选择选项的id
  skuParams.value.skuSaleAttrValueList = AttrList.value.reduce((prev: any, next: any) => {
    if (next.saleIdAndValueId) {
      let [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(':')
      prev.push({
        saleAttrId,
        saleAttrValueId,
      })
    }
    return prev
  }, [])
  let result = await reqAddSku(skuParams.value)
  if (result.code === 200) {
    ElMessage({
      type: 'success',
      message: '添加成功',
    })
    $emit('changeScene', { value: 0, params: '' })
  } else {
    ElMessage({
      type: 'error',
      message: '添加失败',
    })
  }
}
defineExpose({ initSkuData })
</script>

<style></style>
